*******************************************************************************
*
* GS/OS System Call Manager ("SCuM")
* 
* Authors:
*
*	Mike Askins	Jan, 1987 - Oct, 1988	
*	Jim Jatczynski	Jan, 1987 - Oct, 1988
*	Bryan Atsatt	Oct, 1988 - Sep, 1989
*	Cary Farrier	Sep, 1989 - Present
*
* 	a.k.a. "SCuM Lords"
*
* Copyright (c) 1987-89 Apple Computer, Inc.  All Rights Reserved
*******************************************************************************
		eject
******************************************************************************
*                                                                            *
*                  System Call Manager Modification History                  *
*                                                                            *
*                                                                            *
* Date       Ver      Who     Description                                    *
* -------------------------------------------------------------------------- *
* 18 Dec 87  0.10     MSA     Initial State                                  *
*                     JJ      Bouncing Rework                                *
*                     MSA     Changes in Shutdown, release_vcr, swapout      *
*                     JJ&MSA  Added alerts, sessions                         *
* 01 Feb 88  0.01a01          ** Alpha 01 Release **                         *
* 02 Feb 88           MSA     Fix init so that the $00 terminator is placed  *
*                              correctly when making the '*' prefix; removed *
*                              out of date code.                             *
*                             Changed alerts so that if the textbox shows    *
*                              only one button, the prompt will be just the  *
*                              return character.                             *
*                             Changed swapout so that the sense of the swap- *
*                              out bit is reversed.  Added 'write protect'   *
*                              flag in to vcr status.                        *
*                     JJ      Changed the system death routine so that it    *
*                              calls the alert window stuff.                 *
* 08 Feb 88  0.02a01          ** Alpha 02 Release **                         *
* 16 Feb 88  0.03a01  JJ&MSA  Signals added; modified error message file;    *
*                              fixed findvcr, findfcr so that refs to large  *
*                              IDs fail correctly; bug fix in format/erase.  *
* 25 Feb 88  0.03a02  JJ&MSA  Signal bugs fixed; changed over to ':' seps    *
*                              and widened the range of legal pathname chars *
* 29 Feb 88           MSA     Changed class 0 get_prefix so that it nulls    *
*                              the output string upon encountering a slash   *
*                              in the prefix.  Should help existing apps to  *
*                              fail more gracefully.                         *
* 03 Mar 88  0.04a01          ** Alpha 04 Release **                         *
* 21 Mar 88           MSA     "Premature Ejection" message reworded in       *
*                              English message file.  Allocate changed to    *
*                              purge cache if an out of memory error is      *
*                              encountered.                                  *
* 21 Mar 88  0.05a01          ** Alpha 05 Release **                         *
* 21 Mar 88           JJ      Changes made to alert mgr to compensate for    *
*                              spec change in window mgr.  English msg file  *
*                              changed for the same reason.  Alerts mod to   *
*                              accomodate new msgs for format and erase.     *
* 21 Mar 88           MSA     Bug fixed in DynaLists- change to correct      *
*                              behavior in 'out of id' scenarios; changed    *
*                              length of prefixes 0-7 to 64 to match ProDOS  *
*                              16's ill-advised behavior (When P16 adds a    *
*                              slash it can overwrite the buffer given it -  *
*                              manual says up to 64 bytes can be returned.   *
*                     MSA     Bug in 'out of memory' failure of alloc_record *
*                              fixed; reset_cache call added (no doc)        *
* 21 Mar 88  0.06a01          ** Alpha 06 Release **                         *
* 04 May 88           JJ      Support for new format options added.  Reset   *
*                              now forces reboot.                            *
* 04 May 88  0.07a01          ** Alpha 07 Release **                         *
* 02 Jun 88           JJ      English message file: added 'SCSI ROM Version' *
*                              error message.  Added call 'GetStackedID'     *
* 02 Jun 88  0.08a01          ** Alpha 08 Release **                         *
* 14 Jun 88           JJ      Added os_shutdown call.  Added related error   *
*                              in English error msg file.                    *
* 14 Jun 88  0.09a01          ** Alpha 09 Release **                         *
* 28 Jun 88           MSA     Fixed bug in find_record which did not catch   *
*                              ids with the top two bits set and return w/   *
*                              error. Changed lock/unlock sys serv calls to  *
*                              allow proper nested operation.  Changed       *
*                              create_seg so that if GS/OS memory is locked  *
*                              any segment made is also locked.  Low level   *
*                              lock code changed to preserve ZP loc 'hand'.  *
* 27 Jun 88           JJ      Provide a large stack for AlertWindow- uses    *
*                              GS/OS stack and preserves GS/OS zpage (and    *
*                              one extra page for good measure) around the   *
*                              call.                                         *
* 28 Jun 88           JJ/MSA  Change Get_FST_Info script so that it does not *
*                              check for a valid refnum parameter.           *
* 29 Jun 88  0.10a01          ** Alpha 10 Release **                         *
*                                                                            *                                                                                                        *
******************************************************************************

******************************************************************************
*									     *
*									     *
* 			>>	  BIG GAP	<<			     *									     *
*									     *
* The history was not maintained from June 30, 1988 to August 16, 1988 when  *									     *
* the first external release was made on System Disk 4.0.  I have picked up  *									     *
* the history from release date onwards...				     *									     *
*                                                                            *                                                                                                        *
******************************************************************************

*******************************************************************************
*
* REVISION HISTORY:
*
* Date	   By		Description
* -------- ------------ -------------------------------------------------------
* 8/16/88  JJ/MSA	02.00 release.
* 11/1/88  BPA		Conversion to MPW/MAX.
* 11/16/88 BPA		Added parse_pathname system service call.  Modified
*			set_level and get_level such that two distinct ranges
*			of levels can be used: system and user (default).
* 11/17/88 BPA		Added os_event handler.
* 11/18/88 BPA		Added os_add_queue and os_delete_queue calls to support
*			the os_event handler.
* 11/21/88 BPA		Added set_prefix_33 procedure for the '@' prefix, and
*			put in supporting changes.
* 11/28/88 BPA		Restructured source into 1 file (scm.src).
* 11/29/88 BPA		Fixed allocation length in set_prefix_33 to account for
*			length word of string.  Added prefix # range check to
*			get_prefix.
* 11/30/88 BPA		Fixed cvt0to1 to handle null string correctly by
*			clearing high byte. Fixed set_prefix_33 so does not
*			count trailing zero in length word. Fixed get_prefix:
*			range check was in the wrong place.  Added 
*			chk_vol_change so will post volume_change events.
* 12/1/88  BPA		Fixed chk_vol_change so walks vcr list correctly.
*			Changed os_event so sets direct page to zero before
*			calling notify_proc.
* 12/1/88  BPA		02.01d5 release.
* 12/6/88  BPA		Restored inc_busy_flag call in oscall_module and
*			removed it from return_from_lc.  Changed mm_attributes
*			so _newhandle only requests that no special memory be
*			used.  Modified os_shutdown so returns parm_range_err
*			if shutdown_flag has any bits set other than 0 & 1.
* 12/7/88  BPA		Fixed os_shutdown to check for error on GetFirstWindow
*			call (fixes BRC#38977).
* 12/8/88  BPA		Fixed os_shutdown so skips all window manipulation if
*			window manager is not active.
* 12/13/88 BPA		Added CLC after string copy in set_prefix_33.  Changed
*			set_level so does not do range check for system_level.
* 1/3/89   BPA		Changed seg_3_max from $bb00 to $ba00 to give Ray some
*			space.  Commented out scripts for device calls.  Added
*			special case in check_call for device calls which skips
*			sets the type and exits since Ray checks all the
*			remaining parameters.  Changed s_parse_path to set data
*			bank.
* 1/4/89   BPA		Added logic to avoid doing chk_vol_change when there
*			are no notify_procs in the queue which want the event
*			and/or when the call was handled by a character fst.
* 1/5/89   BPA		Modified chk_vol_change so table is a bit map.  Added
*			support for the new device dispatcher.
* 1/9/89   BPA		Modified s_sys_death to clear system busy flag so that
*			CDA's will be enabled during message.  Changed 
*			do_type_7 so calls init_pack_end when done with format
*			or erase so that message can be erased.  Added logic to
*			avoid bouncing on pathname calls to character devices.
* 1/11/89  BPA		Changed logic which avoids bouncing on pathname calls
*			to character devices so uses a new global which is set
*			by a call to get_dnum (char_dev_flag).
* 1/12/89  BPA		Changed add_fst so will not install fst if it fails on
*			the startup call.  Added init_path_vars routine and 
*			modified s_parse_path, xlate_script, and fe_xlate to
*			use it.  Changed findvcr system service vector to point
*			to findvcrcase so searches will be case insensitive.
*			Changed call_fsts so simulates error on volume call to
*			the character fst;  this was necessary because of the
*			changes which avoided bouncing on pathname calls to
*			character devices.
* 1/13/89  BPA		Added set/clear fst_type_flag logic to call_fst.
* 1/16/89  BPA		Changed os_event event code storage to local variable
*			and saved link_pointer across call to notify_proc.
* 1/17/89  BPA		Modified get_fst_info to fake an extra fst for ISO 9660
*			if the High Sierra FST is present.
* 1/18/89  BPA		Modified add_fst to update the iso_fst_header by
*			copying the High Sierra FST header info.
* 1/19/89  BPA		Added two preference bits to sys_prefs: bit 14 controls
*			which dialog the mount_message uses (0=normal, 1=no
*			cancel button), bit 13 controls suppression of error
*			dialogs (dialogs with only 1 button;  1=suppress).
*			Started a miscellaneous data area in the misc_seg
*			and moved iso_fst_header there because above changes
*			caused an overflow of the main segment.  Also moved
*			FST Table Data Structures to misc_seg.  Changed
*			default system preferences to enable the mount volume
*			message.  Modified save_screen and restore_screen to
*			switch in text page 1 before save or restore.
* 1/23/89  BPA		Changed os_shutdown so it closes any open files after
*			posting the shutdown event;  this will ensure that
*			the disk is clean.
* 1/24/89  BPA		Removed volume name parameter from chk_vol_change.
*			Added logic to avoid doing volume_change notification
*			on close or flush calls when no data was written.
*			Changed chk_vol_change back to using a byte map rather
*			than a bit map to enhance performance.
* 1/25/89  BPA		Added d_rename call which is passed directly to the
*			device manager.
* 1/30/88  BPA		Added network_error to bouncing call #'s (is_bouncer).
* 1/31/89  BPA		Commented out both occurences of stz |unclaimed_count
*			done when an interrupt was handled (BRC#37945).  This 
*			means that the unclaimed interrupt counter is only
*			cleared at startup;  if it ever rolls over, we go to 
*			system death.
* 2/9/89   BPA		Added get_std_ref_num call.  Modified xlate_path so if
*			is partial path & prefix 0 is null, will use prefix 8.
*			Fixed call_fsts so generates not_block_device error
*			for opening character devices with paths that are more
*			than a device name.
* 2/14/89  BPA		Changed shutdown_scm so does not unload error messages.
* 2/15/89  BPA		Changed s_report_fatal so calls alternate entry point
*			in os_shutdown (shutdown_reboot) which forces restart
*			after shutdown.
* 2/16/89  BPA		Changed add_fst to return fst_load_fail or error
*			returned by fst from startup.
* 2/17/89  BPA		Modified alloc_fcr so will always store full pathname
*			in fcr.  Moved a bunch of small routines from gim_seg
*			to misc_seg to free up space in gim_seg.
* 2/20/89  BPA		Added insert_vol_name code for expand_path so will
*			convert device name/number to volume name for output.
* 2/21/89  BPA		Added fst_tbl_ptr so Ray's DA can find FST headers.
*			Added get_ref_num call.  Moved release_v_ptrs from
*			scm_main segment to misc_seg.
* 2/22/89  BPA		Changed seg_3_max to equal globals_end to gain more
*			code space in misc_seg.  Added access parameter as
*			output from get_ref_num call.  Added install_int_vec,
*			remove_int_vec, and interrupt_patch along with the 
*			calls to them needed to ensure that signals which do
*			not get dispatched on exit from os call will get 
*			dispatched with interrupts enabled.
* 2/23/89  BPA		Moved interrupt_patch from misc_seg to oscall_seg to
*			ensure that it doesn't end up in the LC area.  Bumped
*			seg_0_max to $a280 (had to change cache manager's start
*			address to do this).  Added get_ref_info call.  Changed
*			dispatch_signal so will always dispatch all signals
*			regardless of interrupt state.  Modified xlate_path
*			to scan string for nulls and return bad_path_syntax if
*			found (fixes BRC# 41728).
* 2/26/89  GRT		Changed Xlate_Path so a zero length string bypasses
*			the scan_for_null routine.  This fixes a bug where
*			set prefix and quit were returning an error if a 
*			null length string was used as input.
* 2/27/89  BPA		Changed how unclaimed_count is handled:  now stops if
*			it ever reaches $ffff.  We check this condition on
*			entry to every OS call and display fatal error message
*			and restart system if found.  Changed s_report_fatal
*			so forces preferences to state in which errors are
*			reported.  Moved vidtab to misc_data to free up some
*			space in scm_seg_1.
* 2/28/89  BPA		Changed deref_int so saves and restores its direct page
*			usage and uses seg_tab_copy rather than seg_tab which
*			lives on direct page and is unreliable during an
*			interrupt.  Moved s_parse_path to misc_seg to free up
*			some space in seg_1.  Removed chk_vol_change call and
*			all supporting logic (if this feature is needed, it
*			will have to be supported by FSTs).  Changed s_signal
*			so sets direct page.
* 3/1/89   BPA		Fixed bug in os_shutdown which caused it to always do
*			powerdown.  Re-ordered os_shutdown so that all
*			tool calls occur before the OS shutdown calls.
* 3/2/89   BPA		Changed os_shutdown to set warm_cold_flag to cold.
*			Changed shutdown_scm, init_scm, init_int and
*			init_signal to respect warm_cold_flag.
* 3/3/89   BPA		Fixed bug in xlate_path which caused crash if path is
*			null.
* 3/6/89   BPA		Re-ordered source so segments are not broken up and are
*			in low to high memory order.  Renamed s_parse_path to
*			parse_path.
* 3/8/89   BPA		Added macros to dump out free space from each segment.
*			Fixed bug in os_shutdown which did not trash memory
*			manager power up byte when requested if the system is
*			in text mode.
* 3/9/89   BPA		Removed pcount and class checking from expand_path and
*			do_quit and put in script to free some space.  Changed
*			s_report_fatal to set direct page (fixes MJS261LWA).
*			Modified do_type_7 (format and erase calls) so checks
*			for open files on target volume and returns a file_busy
*			error if any open (fixes BRC 36724).  Modified
*			do_get_last_dev call so handles only class 0 case
*			(script ensures this is the only one that gets thru).
*			Merged call_proc_data with misc_data to gain space in
*			scm_seg_2.
* 3/13/89  BPA		Fixed get_ref_num to check pcount correctly.
* 3/27/89  BPA		Fixed unlock_mem to save value of lock_flag.  Added 
*			code to lock all of GS/OS managed memory on call entry
*			and unlock it on exit.
* 4/3/89   BPA		Changed shudown_scm so releases all VCRs on warm
*			shutdown after closing all files (BRC#47003).  Changed
*			minimum pcount for set_prefix to 2 (BRC#46822).  Moved
*			is_bouncer from scm_seg_2 to scm_seg_1 to free space.
* 4/7/89   BPA		Removed init_path_vars call from parse_path and added
*			system service call to init_path_vars which must now
*			be called once before calling parse_path (BRC#46696).
* 4/10/89  BPA		Fixed s_signal so cleans up stack correctly on error.
*			Modified shutdown_scm so releases all vcrs on cold
*			shutdown as well as warm shutdown.
* 4/11/89  BPA		Fixed shutdown_scm so correctly releases ALL vcrs.
*			Changed init_path_vars so must jsl to it so that
*			init_parse_path works.  Re-ordered os_shutdown sequence
*			so that _DeskShutDown always occurs and so that no
*			shutdown processes are done after the powerdown dialog.
*			Fixed disp_common_int so handles unclaimed ints
*			correctly.
* 4/17/89  BPA		Added init_parsepath routine to ensure that data bank
*			set correctly.
* 4/18/89  SEG & ELC    Corrected the bold faced lie that appears above:
*			init_path_vars is called with a jsr, not jsl!  (Hey you
*			guys, I wasn't done yet!!).
* 4/19/89  BPA		Added grab_fixed_seg entry point to grab_mm_seg to
*			allocate fixed memory (used for vect_disp_hand).
* 4/21/89  BPA		Changed init_gim and init_list so allocates larger list
*			for seg_tab so that will be less likely to move out
*			into middle of memory.  Modified init_parsepath so that
*			entry with carry clear does init, carry set releases
*			vps used during parse_path.
* 4/28/89  BPA		Changed get_prefix to not force upper case on class 0
*			calls.
* 5/2/89   BPA		Added close_all_files routine which does end_sessions
*			and close all with system_level at zero.  Modified
*			os_shutdown to call close_all_files.  Removed all file
*			closing logic from shutdown_scm because Gquit has
*			already shutdown the device dispatcher by the time
*			shutdown_scm gets called and close requires it to be
*			active.  Gquit must now call close_all_files before
*			calling shutdown_scm.
* 5/3/89   BPA		Modified close_all_files so sets data bank reg 'cuz
*			Gquit doesn't and so sets sys_prefs to force one button
*			mount dialogs.
* 5/11/89  BPA		Modified report_fatal to check a new flag (e1_gsos_ok,
*			maintained by Gquit);  if zero, will skip all shutdown
*			code and just force reboot.  Changed gsos_lc_entry
*			so that if stack pointer is already in the OS area, it
*			is not forced to stack_top.  This solves some problems
*			on shutdown.  Also changed script for get_name so
*			checks for $00/0000 buffer address.
* 5/18/89  BPA		Moved xlate_script to segment 0 to free up space in
*			segment 2.  Added code to call_fsts to call
*			loader_rename on successful change_path call.  Moved
*			event queue header to segment 2.  Changed
*			insert_vol_name so exits with rtl.  Put loader_rename
*			code in gquit.src cuz wouldn't fit in SCM!  Added
*			xlate_path2_l for loader_rename.  Added do_alert_window
*			in gquit.src to handle removing & restoring CR's where
*			needed.
* 5/30/89  BPA		Changed version number to 3.00, final.
* 6/2/89   BPA		Changed get_ref_num so does not return access when
*			pcount is 2.
*
* 6/17/89  BPA	------	3.00 Release ------------------------------------------
*
* 6/27/89  BPA		Added new segment which lives in what used to be GQuit
*			space (E1DC00-E1DFFF).  Brought loader_rename and
*			do_alert_window back out of Gquit.src and into this
*			new segment.  Moved s_mount_msg to scm_seg_4.  Changed
*			version numbers to 3.01.
* 7/18/89  BPA		Modified force_lock so patches out system service
*			entry for lock_mem and modified force_unlock so
*			restores system service entry point.
* 7/19/89  BPA		Moved vidtab to scm_seg_4 to free up space and modified
*			gotoxy to use long addressing.  Removed lock_mem and
*			unlock_mem calls from do_type_7.  Modified make_seg and
*			deallocate to remember highest used position in
*			seg_tab.  Modified unlockmem and lockmem to be much
*			faster (including some self-modifiying code), and
*			rather than walking the entire list, it now stops one
*			past the highest used position.
* 7/26/89  BPA		Fixed bug in unlockmem which my version of the linker
*			did not report (branch out of range)!
*
* 	   CAF	------	3.0.3 Release ------------------------------------------
*
* 9/13/89  CAF		Fixed bug in os_event where a local variable was not
*			being preserved across calling the notify_procs.
* 9/15/89  CAF		Fixed bug that I introduced on 9/13/89.  I moved the
*			code which pushes the local variable onto the stack to
*			a location where it was safe for me to trash the A
*			register.
* 9/18/89  CAF		Fixed a second bug I introduced on 9/15/89, where I
*			had added 4 bytes to the stack and forgot to
*			change the stack relative offsets when loading the
*			a,x,y registers.
* 09/26/89 CAF		Moved _DeskShutDown to before the dispatch of the
*			gsos_shutdown event to the notification queue.  This
*			fixed a bug where sometimes the system would hang after
*			an OS_Shutdown call, due to the Desk Manager making a
*			call to the Resource Manager, which had been shut down
*			through the notification_queue already.  Changed version
*			numbers to 3.03.
*
* 	   CAF	------	3.0.4 Release ------------------------------------------
*
* 11/28/89 CAF		Incorporated a bug fix by Matt Gulick into disp_common_int.
*			This fixes a problem with 65536 unclaimed interrupts being
*			reported, when this was not the case.  Also changed version
*			numbers to 3.04.
*
* 	   CAF	------	3.0.5 Release ------------------------------------------
*
* 12/14/89 CAF		Changed handling of 65536 unclaimed interrupts.  Now, instead
*			of reporting it as a fatal system error, a message
*			will be displayed to the user and things will continue
*			on as normal.
* 01/09/90 CAF		Updated version #'s to 3.05
* 01/11/90 CAF		Incorporated Rob's quick.console init into the OS, and
*			optimized character output.  Added new variable "char_index",
*			the index of the char fst into the fst table, for faster
*			character io.
*
* 	   CAF	------	3.0.6 Release ------------------------------------------
*
* 01/24/90 CAF		Added an unbind_int system service call at $01/FDC8 so
*			that Matt's driver could perform and unbind_int at
*			shutdown time.
* 01/26/90 CAF		Commented out the console io speedups in order to 
*			create a build version for Cocoon.
* 04/06/90 CAF		Fixed bugs in make_seg, deallocate, and lock/unlock_mem which
*			were using 16 bit addressing when the data bank register
*			was not set correctly, so SCM was stomping on memory
*			and not unlocking some handles.
* 06/15/90 CAF		Removed display of unclaimed interrupt dialog box for
*			system disk 5.0.3.  Fixed a bug where the carry was not set
*			if the user selected cancel from an Init Manager dialog.
* 06/20/90 GAB		Since the JSR to the unclaimed interrupt dialog box was
*			removed in the last change, I went ahead and removed the
*			preceeding code which checked to see if the number of
*			unclaimed interrupts had been exceeded.  Also fixed a couple
*			of minor problems that were put in with the new code that
*			saves the carry bit after a system call.  Since the new
*			section of code is shorter than the original version, I
*			added a DS.B statement which automatically makes it the same
*			length to keep all of the following code in place.  Changes
*			marked by {GAB}.
* 09/25/90 CAF		Uncommented all the console speedup routines, now character
*			output is about 28% faster, but could use some more work
*			particularly in the find_fcr area.  Optimized some code
*			in os_shutdown to free up more space, as marked by {CAF}.
*
*******************************************************************************
